//
//  SpriterNode.h
//  Tank
//
//  Created by Young Peter on 9/1/12.
//  http://weibo.com/young40
//  revised by qibinyi@gmail.com,  20130111
//
//
//

#ifndef __Tank__SpriterNode__
#define __Tank__SpriterNode__

#include "cocos2d.h"

USING_NS_CC;


#include "cAnimationManager.h"

#define RECT_DEFAULT_VALUE 99999

class SpriterNode : public CCNode 
{
public:
    SpriterNode();
    ~SpriterNode();
    
    //static SpriterNode* create(const char *scmlFile);
    static SpriterNode* create(const char *scmlFile, const char *plistFile);
    

    
    void runAnimation(const char* animation , bool bLoop );
    bool isAnimFinished( );
    bool isCollided( float x , float y);
    
    bool getIsFlipX();
    void setIsFlipX(bool b);
    
    /** pSpeed is in percentage, it can be
        1.0 (Normal Speed)
        5.0 (5x speed)
        0.25 (0.25x speed)
        many more
     
        This value can be manually grabbed from Spriter editor, but measure in percentage.
    **/
    double getPlaybackSpeed();
    void setPlaybackSpeed(double pSpeed);
    
    void setModuleMapper( const char*  anim , const char* module_src, const char* module_dist ) {
        
        CCDictionary* dict = (CCDictionary*)_dict_moduleMapper->objectForKey(anim);
        
        if (dict == NULL) {
            dict = new CCDictionary();
            dict->autorelease();
            _dict_moduleMapper->setObject(dict, anim);
        }
        
        dict->setObject(CCString::create(module_dist), module_src);
    }
    
    void setAnimation(cAnimation* anim);
    
private:
    
    const char*  doModuleMapper( const char*  anim , const char* module_src ) {
        CCDictionary* dict = (CCDictionary*)_dict_moduleMapper->objectForKey(anim);
        if (dict == NULL)
            return module_src;
        
        CCString* module_dist = ((CCString*)dict->objectForKey(module_src)) ;
        if (module_dist == NULL) {
            return module_src;
        }
        else {
            return module_dist->getCString();
        }
    }
#if CC_SPRITE_DEBUG_DRAW == 1
    virtual void draw(void);
#endif
    cAnimation* cAnim;

    CCArray *_spriteNodes;
    CCDictionary* _dict_moduleMapper ;
    
    
    SpriterAnimation *_curAnimation;
    SpriterMainlineKey *_curKeyFrame;
    SpriterMainlineKey *_nextKeyFrame;
    
    double _duration;
    int _frameIndex;
    bool _bLoop ;
    bool _bFinished ;
    float _rect[4];
    
    bool _bRectNeedUpdate; 

    
    //bool _useBatchNode;
    CCSpriteBatchNode *_batchNode;
    
    
    
    void update(float dt);
    double interpolate(double a, double b, double f);
    
    bool _isFlipX;
    double _playbackSpeed;
};


#endif /* defined(__Tank__SpriterNode__) */
